﻿Public Class CcapdevTypeForm

    Private Sub CompressorGroupManageForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDevTypeGroup()
    End Sub

    Private _pi As ProjectItem

    Private Sub LoadDevTypeGroup()

        _pi = MainForm.pi


        Dim bFirst As Boolean = True

        For Each dti As CcapDevItem In CcapDevItemService.GetCcapDevTypeList(_pi)
            AddGroupControl(dti)
        Next

        If Me.FlowLayoutPanel1.Controls.Count > 0 Then
            CType(Me.FlowLayoutPanel1.Controls(0), CcapDevTypeControl).groupSelected = True
            CType(Me.FlowLayoutPanel1.Controls(0), CcapDevTypeControl).Focus()
            Me.btnModifyGroup.Enabled = True
            Me.btnDeleteGroup.Enabled = True
        Else
            Me.btnModifyGroup.Enabled = False
            Me.btnDeleteGroup.Enabled = False
        End If

    End Sub

    Private Sub AddGroupControl(ByVal dti As CcapDevItem)
        Dim c As New CcapDevTypeControl

        c.dtiItem = dti

        c.SetEnable(False)
        c.groupSelected = False



        AddHandler c.GroupSelectEvent, AddressOf CompressorGroupControl_GroupSelectEvent


        Me.FlowLayoutPanel1.Controls.Add(c)
    End Sub


 


    Private Sub btnAddGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddGroup.Click
        Dim frm As New CcapDevForm(_pi.ProjectID)
        Dim dt As CcapDevItem
        Dim tip As String

        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            dt = frm.dti

            If CcapDevItemService.AddCCapDevList(frm.dti) = True Then

                AddGroupControl(dt)


                ' _isModified = True


                msg.Show("设备添加成功")
            Else
                tip = String.Format("增加设备【{0}】失败", frm.dti.NetId)
                msg.ShowError(tip)
            End If
        End If

    End Sub

    Private Sub CompressorGroupControl_GroupSelectEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
        For Each c As CcapDevTypeControl In Me.FlowLayoutPanel1.Controls
            c.groupSelected = False
            c.SetEnable(False)
        Next

        CType(sender, CcapDevTypeControl).groupSelected = True
        Me.btnDeleteGroup.Enabled = True
        Me.btnModifyGroup.Enabled = True

    End Sub

    Private Function GetSelectedControl() As CcapDevTypeControl
        Dim item As CcapDevTypeControl = Nothing

        For Each c As CcapDevTypeControl In Me.FlowLayoutPanel1.Controls
            If c.groupSelected Then
                item = c
                Exit For
            End If
        Next

        Return item
    End Function
 
    Private Sub btnModifyGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModifyGroup.Click
        Dim ctl As CcapDevTypeControl
        ' Dim tip As String

        ctl = GetSelectedControl()

        If ctl Is Nothing Then
            Exit Sub
        End If


        Dim frm As New CcapDevForm(ctl.dtiItem, _pi.ProjectID)



        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            'ctl.FlowLayoutPanel1.Controls.Clear()
            'ctl.FlowLayoutPanel2.Controls.Clear()
            'ctl.FlowLayoutPanel3.Controls.Clear()

            'ctl.dtiItem = frm.dti
            Me.FlowLayoutPanel1.Controls.Clear()

            ' AddGroupControl(dt)
            For Each dti As CcapDevItem In CcapDevItemService.GetCcapDevTypeList(_pi)
                AddGroupControl(dti)
            Next


            msg.Show("修改成功")


          
        Else

        End If
    End Sub

    Private Sub btnDeleteGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteGroup.Click
        Dim ctl As CcapDevTypeControl
        ctl = GetSelectedControl()

        If ctl Is Nothing Then
            Exit Sub
        End If


        If Integer.Parse(Trim(ctl.tb_netid.Text)) <> CcapDevItemService.DevTypeMaxNetid(_pi.ProjectID) Then
            msg.Show("请按顺序从最大设备开始删除")
            Exit Sub
        End If


        Dim tip As String
        tip = String.Format("是否要删除设备【{0}】的所有配置信息？（删除前请确认已将配置对应端口清0！）", ctl.dtiItem.NetId)
        If msg.ShowYesNo(tip) = Windows.Forms.DialogResult.Yes Then
            If CcapDevItemService.DelCcapDevRefTypeList(ctl.dtiItem, Me.FlowLayoutPanel1.Controls.Count) = True Then

                Me.FlowLayoutPanel1.Controls.Clear()

                ' AddGroupControl(dt)
                For Each dti As CcapDevItem In CcapDevItemService.GetCcapDevTypeList(_pi)
                    AddGroupControl(dti)
                Next


                msg.Show("删除成功")
            Else
                tip = String.Format("删除设备【{0}】失败", ctl.dtiItem.NetId)
                msg.ShowError(tip)
            End If
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
End Class